package com.ruoyi.plus.pub.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.plus.pub.service.IPubBannerService;
import com.ruoyi.plus.pub.domain.PubBanner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 系统轮播图Controller
 */
@RestController
@RequestMapping("/pub/banner")
public class PubBannerController extends BaseController {
    @Autowired
    private IPubBannerService pubBannerService;

    /**
     * 查询系统轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('pub:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(PubBanner pubBanner) {
        startPage();
        List<PubBanner> list = pubBannerService.selectPubBannerList(pubBanner);
        return getDataTable(list);
    }

    /**
     * 导出系统轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('pub:banner:export')")
    @Log(title = "系统轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PubBanner pubBanner) {
        List<PubBanner> list = pubBannerService.selectPubBannerList(pubBanner);
        ExcelUtil<PubBanner> util = new ExcelUtil<PubBanner>(PubBanner.class);
        util.exportExcel(response, list, "系统轮播图数据");
    }

    /**
     * 获取系统轮播图详细信息
     */
    @PreAuthorize("@ss.hasPermi('pub:banner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pubBannerService.getById(id));
    }

    /**
     * 新增系统轮播图
     */
    @PreAuthorize("@ss.hasPermi('pub:banner:add')")
    @Log(title = "系统轮播图添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PubBanner pubBanner) {
        return toAjax(pubBannerService.save(pubBanner));
    }

    /**
     * 修改系统轮播图
     */
    @PreAuthorize("@ss.hasPermi('pub:banner:edit')")
    @Log(title = "系统轮播图修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody PubBanner pubBanner) {
        return toAjax(pubBannerService.updateById(pubBanner));
    }

    /**
     * 删除系统轮播图
     */
    @PreAuthorize("@ss.hasPermi('pub:banner:remove')")
    @Log(title = "系统轮播图删除", businessType = BusinessType.DELETE)
    @PostMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pubBannerService.removeByIds(Arrays.asList(ids)));
    }

    /* ---------------------------------------------------------------------------------------- */

}
